using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Client
{
public partial class AIPlayForm : Form
{
private const int rectSize = 33;
private const int edgeCount = 15;
private enum Horse { none = 0, BLACK, WHITE };
private Horse[,] board = new Horse[edgeCount, edgeCount];
private Horse nowPlayer = Horse.BLACK;
private Horse aiPlayer, userPlayer;
private int targetX, targetY;
private int limit = 1;
private bool playing = false;
private bool judge()
{
for (int i = 0; i < edgeCount - 4; i++)
for (int j = 0; j < edgeCount; j++)
if (board[i, j] == nowPlayer && board[i + 1, j] == nowPlayer && board[i + 2, j] == nowPlayer &&
board[i + 3, j] == nowPlayer && board[i + 4, j] == nowPlayer)
return true;
for (int i = 0; i < edgeCount; i++)
for (int j = 4; j < edgeCount; j++)
if (board[i, j] == nowPlayer && board[i, j - 1] == nowPlayer && board[i, j - 2] == nowPlayer &&
board[i, j - 3] == nowPlayer && board[i, j - 4] == nowPlayer)
return true;
for (int i = 0; i < edgeCount - 4; i++)
for (int j = 0; j < edgeCount - 4; j++)
if (board[i, j] == nowPlayer && board[i + 1, j + 1] == nowPlayer && board[i + 2, j + 2] == nowPlayer &&
board[i + 3, j + 3] == nowPlayer && board[i + 4, j + 4] == nowPlayer)
return true;
for (int i = 4; i < edgeCount; i++)
for (int j = 0; j < edgeCount - 4; j++)
if (board[i, j] == nowPlayer && board[i - 1, j + 1] == nowPlayer && board[i - 2, j + 2] == nowPlayer &&
board[i - 3, j + 3] == nowPlayer && board[i - 4, j + 4] == nowPlayer)
return true;
return false;
}
private void refresh()
{
for (int i = 0; i < edgeCount; i++)
for (int j = 0; j < edgeCount; j++)
board[i, j] = Horse.none;
this.boardPicture.Refresh();
int rand = new Random().Next(1, 3);
if (rand == 1) aiPlayer = Horse.BLACK;
else aiPlayer = Horse.WHITE;
userPlayer = ((aiPlayer == Horse.BLACK) ? Horse.WHITE : Horse.BLACK);
nowPlayer = Horse.BLACK;
if (nowPlayer == userPlayer) status.Text = "당신의 차례입니다.";
else status.Text = "게임이 시작되었습니다.";
}
private void playButton_Click(object sender, EventArgs e)
{
if (!playing)
{
refresh();
playing = true;
playButton.Text = "재시작";
}
else
{
refresh();
}
if (nowPlayer == aiPlayer)
{
ai_Attack();
}
}
public AIPlayForm()
{
InitializeComponent();
}
private void boardPicture_Paint(object sender, PaintEventArgs e)
{
Graphics gp = e.Graphics;
Color lineColor = Color.Black;
Pen p = new Pen(lineColor, 2);
gp.DrawLine(p, rectSize / 2, rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2);
gp.DrawLine(p, rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize / 2);
gp.DrawLine(p, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2);
gp.DrawLine(p, rectSize * edgeCount - rectSize / 2, rectSize / 2, rectSize * edgeCount - rectSize / 2, rectSize * edgeCount - rectSize / 2);
p = new Pen(lineColor, 1);
for (int i = rectSize + rectSize / 2; i < rectSize * edgeCount - rectSize / 2; i += rectSize)
{
gp.DrawLine(p, rectSize / 2, i, rectSize * edgeCount - rectSize / 2, i);
gp.DrawLine(p, i, rectSize / 2, i, rectSize * edgeCount - rectSize / 2);
}
}
private void ai_Attack()
{
AlphaBetaPruning(0, -1000000, 1000000);
board[targetX, targetY] = aiPlayer;
Graphics g = this.boardPicture.CreateGraphics();
if (nowPlayer == Horse.BLACK)
{
SolidBrush brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, targetX * rectSize, targetY * rectSize, rectSize, rectSize);
}
else
{
SolidBrush brush = new SolidBrush(Color.White);
g.FillEllipse(brush, targetX * rectSize, targetY * rectSize, rectSize, rectSize);
}
if (judge())
{
if (nowPlayer == aiPlayer) status.Text = "AI 플레이어의 승리입니다.";
else status.Text = "당신의 승리입니다.";
playing = false;
playButton.Text = "게임시작";
}
else
{
nowPlayer = ((nowPlayer == Horse.BLACK) ? Horse.WHITE : Horse.BLACK);
}
}
private void boardPicture_MouseDown(object sender, MouseEventArgs e)
{
if (nowPlayer == aiPlayer) return;
if (!playing)
{
MessageBox.Show("게임을 실행해주세요.");
return;
}
Graphics g = this.boardPicture.CreateGraphics();
int x = e.X / rectSize;
int y = e.Y / rectSize;
if (x < 0 || y < 0 || x >= edgeCount || y >= edgeCount)
{
MessageBox.Show("테두리를 벗어날 수 없습니다.");
return;
}
if (board[x, y] != Horse.none) return;
board[x, y] = nowPlayer;
if (nowPlayer == Horse.BLACK)
{
SolidBrush brush = new SolidBrush(Color.Black);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
else
{
SolidBrush brush = new SolidBrush(Color.White);
g.FillEllipse(brush, x * rectSize, y * rectSize, rectSize, rectSize);
}
if (judge())
{
if (nowPlayer == aiPlayer) status.Text = "AI 플레이어의 승리입니다.";
else status.Text = "당신의 승리입니다.";
playing = false;
playButton.Text = "게임시작";
}
else
{
nowPlayer = ((nowPlayer == Horse.BLACK) ? Horse.WHITE : Horse.BLACK);
ai_Attack();
}
}
public bool make5mok1(int x, int y)
{
try
{
for (int i = y; i < y + 5; i++)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok2(int x, int y)
{
try
{
for (int i = x, j = y; i < x + 5; i++, j--)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok3(int x, int y)
{
try
{
for (int i = x; i < x + 5; i++)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok4(int x, int y)
{
try
{
for (int i = x, j = y; i < x + 5; i++, j++)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok5(int x, int y)
{
try
{
for (int i = y; i > y - 5; i--)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok6(int x, int y)
{
try
{
for (int i = x, j = y; i > x - 5; i--, j++)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok7(int x, int y)
{
try
{
for (int i = x; i > x - 5; i--)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok8(int x, int y)
{
try
{
for (int i = x, j = y; i > x - 5; i--, j--)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok9(int x, int y)
{
try
{
for (int i = y - 1; i < y + 4; i++)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok10(int x, int y)
{
try
{
for (int i = x - 1, j = y + 1; i < x + 4; i++, j--)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok11(int x, int y)
{
try
{
for (int i = x - 1; i < x + 4; i++)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok12(int x, int y)
{
try
{
for (int i = x - 1, j = y - 1; i < x + 4; i++, j++)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok13(int x, int y)
{
try
{
for (int i = y + 1; i > y - 4; i--)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok14(int x, int y)
{
try
{
for (int i = x + 1, j = y - 1; i > x - 4; i--, j++)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok15(int x, int y)
{
try
{
for (int i = x + 1; i > x - 4; i--)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok16(int x, int y)
{
try
{
for (int i = x + 1, j = y + 1; i > x - 4; i--, j--)
{
if (board[i, j] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok17(int x, int y)
{
try
{
for (int i = y - 2; i < y + 3; i++)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok18(int x, int y)
{
try
{
for (int i = x - 2; i < x + 3; i++)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok19(int x, int y)
{
try
{
for (int i = y + 2; i > y - 3; i--)
{
if (board[x, i] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public bool make5mok20(int x, int y)
{
try
{
for (int i = x + 2; i > x - 3; i--)
{
if (board[i, y] != board[x, y]) return false;
}
}
catch (IndexOutOfRangeException)
{
return false;
}
return true;
}
public int make3mok1(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x, y + 1] && board[x, y] == board[x, y + 2])
{
count = 2;
if (y < edgeCount - 3 && board[x, y] == board[x, y + 3]) return -1;
if (y > 0 && board[x, y] == board[x, y - 1]) return -1;
if (y == edgeCount - 3 || board[x, y + 3] != 0) count--;
if (y == 0 || board[x, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok2(int x, int y)
{
int count = -1;
try
{
if (board[x, y + 1] == 0 && board[x, y] == board[x, y + 2] && board[x, y] == board[x, y + 3])
{
count = 2;
if (y < edgeCount - 4 && board[x, y] == board[x, y + 4]) return -1;
if (y > 0 && board[x, y] == board[x, y - 1]) return -1;
if (y == edgeCount - 4 || board[x, y + 4] != 0) count--;
if (y == 0 || board[x, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok3(int x, int y)
{
int count = -1;
try
{
if (board[x, y + 2] == 0 && board[x, y] == board[x, y + 1] && board[x, y] == board[x, y + 3])
{
count = 2;
if (y < edgeCount - 4 && board[x, y] == board[x, y + 4]) return -1;
if (y > 0 && board[x, y] == board[x, y - 1]) return -1;
if (y == edgeCount - 4 || board[x, y + 4] != 0) count--;
if (y == 0 || board[x, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok4(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y + 1] && board[x, y] == board[x - 2, y + 2])
{
count = 2;
if (x > 2 && y < edgeCount - 3 && board[x, y] == board[x - 3, y + 3]) return -1;
if (x < edgeCount - 1 && y > 0 && board[x, y] == board[x + 1, y - 1]) return -1;
if (x == 2 || y == edgeCount - 3 || board[x - 3, y + 3] != 0) count--;
if (x == edgeCount - 1 || y == 0 || board[x + 1, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok5(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y + 1] == 0 && board[x, y] == board[x - 2, y + 2] && board[x, y] == board[x - 3, y + 3])
{
count = 2;
if (x > 3 && y < edgeCount - 4 && board[x, y] == board[x - 4, y + 4]) return -1;
if (x < edgeCount - 1 && y > 0 && board[x, y] == board[x + 1, y - 1]) return -1;
if (x == 3 || y == edgeCount - 4 || board[x - 4, y + 4] != 0) count--;
if (x == edgeCount - 1 || y == 0 || board[x + 1, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok6(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y + 2] == 0 && board[x, y] == board[x - 1, y + 1] && board[x, y] == board[x - 3, y + 3])
{
count = 2;
if (x > 3 && y < edgeCount - 4 && board[x, y] == board[x - 4, y + 4]) return -1;
if (x < edgeCount - 1 && y > 0 && board[x, y] == board[x + 1, y - 1]) return -1;
if (x == 3 || y == edgeCount - 4 || board[x - 4, y + 4] != 0) count--;
if (x == edgeCount - 1 || y == 0 || board[x + 1, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok7(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y] && board[x - 2, y] == board[x, y])
{
count = 2;
if (x < edgeCount - 1 && board[x, y] == board[x + 1, y]) return -1;
if (x > 2 && board[x, y] == board[x - 3, y]) return -1;
if (x == edgeCount - 1 || board[x + 1, y] != 0) count--;
if (x == 2 || board[x - 3, y] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok8(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y] == 0 && board[x, y] == board[x - 2, y] && board[x - 3, y] == board[x, y])
{
count = 2;
if (x < edgeCount - 1 && board[x, y] == board[x + 1, y]) return -1;
if (x > 3 && board[x, y] == board[x - 4, y]) return -1;
if (x == edgeCount - 1 || board[x + 1, y] != 0) count--;
if (x == 3 || board[x - 4, y] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok9(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y] == 0 && board[x, y] == board[x - 1, y] && board[x - 3, y] == board[x, y])
{
count = 2;
if (x < edgeCount - 1 && board[x, y] == board[x + 1, y]) return -1;
if (x > 3 && board[x, y] == board[x - 4, y]) return -1;
if (x == edgeCount - 1 || board[x + 1, y] != 0) count--;
if (x == 3 || board[x - 4, y] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok10(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y - 1] && board[x, y] == board[x - 2, y - 2])
{
count = 2;
if (x > 2 && y > 2 && board[x, y] == board[x - 3, y - 3]) return -1;
if (x < edgeCount - 1 && y < edgeCount - 1 && board[x, y] == board[x + 1, y + 1]) return -1;
if (x == 2 || y == 2 || board[x - 3, y - 3] != 0) count--;
if (x == edgeCount - 1 || y == edgeCount - 1 || board[x + 1, y + 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok11(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y - 1] == 0 && board[x, y] == board[x - 2, y - 2] && board[x, y] == board[x - 3, y - 3])
{
count = 2;
if (x > 3 && y > 3 && board[x, y] == board[x - 4, y - 4]) return -1;
if (x < edgeCount - 1 && y < edgeCount - 1 && board[x, y] == board[x + 1, y + 1]) return -1;
if (x == 3 || y == 3 || board[x - 4, y - 4] != 0) count--;
if (x == edgeCount - 1 || y == edgeCount - 1 || board[x + 1, y + 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make3mok12(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y - 2] == 0 && board[x, y] == board[x - 1, y - 1] && board[x, y] == board[x - 3, y - 3])
{
count = 2;
if (x > 3 && y > 3 && board[x, y] == board[x - 4, y - 4]) return -1;
if (x < edgeCount - 1 && y < edgeCount - 1 && board[x, y] == board[x + 1, y + 1]) return -1;
if (x == 3 || y == 3 || board[x - 4, y - 4] != 0) count--;
if (x == edgeCount - 1 || y == edgeCount - 1 || board[x + 1, y + 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok1(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x, y + 1] && board[x, y] == board[x, y + 2] && board[x, y] == board[x, y + 3])
{
count = 2;
if (y == edgeCount - 4 || board[x, y + 4] != 0) count--;
if (y == 0 || board[x, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok2(int x, int y)
{
int count = -1;
try
{
if (board[x, y + 1] == 0 && board[x, y] == board[x, y + 2] && board[x, y] == board[x, y + 3] && board[x, y] == board[x, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok3(int x, int y)
{
int count = -1;
try
{
if (board[x, y + 2] == 0 && board[x, y] == board[x, y + 1] && board[x, y] == board[x, y + 3] && board[x, y] == board[x, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok4(int x, int y)
{
int count = -1;
try
{
if (board[x, y + 3] == 0 && board[x, y] == board[x, y + 1] && board[x, y] == board[x, y + 2] && board[x, y] == board[x, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok5(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y + 1] && board[x, y] == board[x - 2, y + 2] && board[x, y] == board[x - 3, y + 3])
{
count = 2;
if (x == edgeCount - 1 || y == 0 || board[x + 1, y - 1] != 0) count--;
if (x == 3 || y == edgeCount - 4 || board[x - 4, y + 4] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok6(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y + 1] == 0 && board[x, y] == board[x - 2, y + 2] && board[x, y] == board[x - 3, y + 3] && board[x, y] == board[x - 4, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok7(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y + 2] == 0 && board[x, y] == board[x - 1, y + 1] && board[x, y] == board[x - 3, y + 3] && board[x, y] == board[x - 4, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok8(int x, int y)
{
int count = -1;
try
{
if (board[x - 3, y + 3] == 0 && board[x, y] == board[x - 1, y + 1] && board[x, y] == board[x - 2, y + 2] && board[x, y] == board[x - 4, y + 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok9(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y] && board[x, y] == board[x - 2, y] && board[x - 3, y] == board[x, y])
{
count = 2;
if (x == edgeCount - 1 || board[x + 1, y] != 0) count--;
if (x == 3 || board[x - 4, y] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok10(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y] == 0 && board[x, y] == board[x - 2, y] && board[x, y] == board[x - 3, y] && board[x - 4, y] == board[x, y]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok11(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y] == 0 && board[x, y] == board[x - 1, y] && board[x, y] == board[x - 3, y] && board[x - 4, y] == board[x, y]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok12(int x, int y)
{
int count = -1;
try
{
if (board[x - 3, y] == 0 && board[x, y] == board[x - 2, y] && board[x, y] == board[x - 1, y] && board[x - 4, y] == board[x, y]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok13(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y - 1] && board[x, y] == board[x - 2, y - 2] && board[x, y] == board[x - 3, y - 3])
{
count = 2;
if (x == edgeCount - 1 || y == edgeCount - 1 || board[x + 1, y + 1] != 0) count--;
if (x == 3 || y == 3 || board[x - 4, y - 4] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok14(int x, int y)
{
int count = -1;
try
{
if (board[x - 1, y - 1] == 0 && board[x, y] == board[x - 2, y - 2] && board[x, y] == board[x - 3, y - 3] && board[x, y] == board[x - 4, y - 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok15(int x, int y)
{
int count = -1;
try
{
if (board[x - 2, y - 2] == 0 && board[x, y] == board[x - 1, y - 1] && board[x, y] == board[x - 3, y - 3] && board[x, y] == board[x - 4, y - 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make4mok16(int x, int y)
{
int count = -1;
try
{
if (board[x - 3, y - 3] == 0 && board[x, y] == board[x - 1, y - 1] && board[x, y] == board[x - 2, y - 2] && board[x, y] == board[x - 4, y - 4]) count = 1;
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make2mok1(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x, y + 1])
{
count = 2;
if (y < edgeCount - 2 && board[x, y] == board[x, y + 2]) return -1;
if (y > 0 && board[x, y] == board[x, y - 1]) return -1;
if (y == edgeCount - 2 || board[x, y + 2] != 0) count--;
if (y == 0 || board[x, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make2mok2(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y + 1])
{
count = 2;
if (x > 1 && y < edgeCount - 2 && board[x, y] == board[x - 2, y + 2]) return -1;
if (x < edgeCount - 1 && y > 0 && board[x, y] == board[x + 1, y - 1]) return -1;
if (x == 1 || y == edgeCount - 2 || board[x - 2, y + 2] != 0) count--;
if (x == edgeCount - 1 || y == 0 || board[x + 1, y - 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make2mok3(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y])
{
count = 2;
if (x > 1 && board[x, y] == board[x - 2, y]) return -1;
if (x < edgeCount - 1 && board[x, y] == board[x + 1, y]) return -1;
if (x == 1 || board[x - 2, y] != 0) count--;
if (x == edgeCount - 1 || board[x + 1, y] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
public int make2mok4(int x, int y)
{
int count = -1;
try
{
if (board[x, y] == board[x - 1, y - 1])
{
count = 2;
if (x > 1 && y > 1 && board[x, y] == board[x - 2, y - 2]) return -1;
if (x < edgeCount - 1 && y < edgeCount - 1 && board[x, y] == board[x + 1, y + 1]) return -1;
if (x == 1 || y == 1 || board[x - 2, y - 2] != 0) count--;
if (x == edgeCount - 1 || y == edgeCount - 1 || board[x + 1, y + 1] != 0) count--;
}
}
catch (IndexOutOfRangeException)
{
return -1;
}
return count;
}
private int evaluate(Horse horse)
{
int sum = 0;
int open3 = 0;
int close3 = 0;
int half3 = 0;
int open4 = 0;
int close4 = 0;
int half4 = 0;
int open2 = 0;
int close2 = 0;
int half2 = 0;
int count;
for (int i = 0; i < edgeCount; i++)
{
for (int j = 0; j < edgeCount; j++)
{
if (board[i, j] == horse)
{
if (make5mok1(i, j) || make5mok2(i, j) || make5mok3(i, j) || make5mok4(i, j) || make5mok5(i, j) ||
make5mok6(i, j) || make5mok7(i, j) || make5mok8(i, j) || make5mok9(i, j) || make5mok10(i, j) ||
make5mok11(i, j) || make5mok12(i, j) || make5mok13(i, j) || make5mok14(i, j) || make5mok15(i, j) ||
make5mok16(i, j) || make5mok17(i, j) || make5mok18(i, j) || make5mok19(i, j) || make5mok20(i, j))
{
return 1000000;
}
count = make4mok1(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok2(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok3(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok4(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok5(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok6(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok7(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok8(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok9(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok10(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok11(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok12(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok13(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok14(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok15(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make4mok16(i, j);
if (count == 2) open4++;
else if (count == 1) half4++;
else if (count == 0) close4++;
count = make3mok1(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok2(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok3(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok4(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok5(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok6(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok7(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok8(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok9(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok10(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok11(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make3mok12(i, j);
if (count == 2) open3++;
else if (count == 1) half3++;
else if (count == 0) close3++;
count = make2mok1(i, j);
if (count == 2) open2++;
else if (count == 1) half2++;
else if (count == 0) close2++;
count = make2mok2(i, j);
if (count == 2) open2++;
else if (count == 1) half2++;
else if (count == 0) close2++;
count = make2mok3(i, j);
if (count == 2) open2++;
else if (count == 1) half2++;
else if (count == 0) close2++;
count = make2mok4(i, j);
if (count == 2) open2++;
else if (count == 1) half2++;
else if (count == 0) close2++;
int middle = edgeCount / 2;
if (i > middle)
{
sum += 500 - ((i - middle) * 20);
}
else
{
sum += 500 - (middle - i) * 20;
}
if (j > middle)
{
sum += 500 - ((j - middle) * 20);
}
else
{
sum += 500 - (middle - j) * 20;
}
}
}
}
sum += open4 * 200000;
sum += half4 * 15000;
sum += close4 * 1500;
sum += open3 * 4000;
sum += half3 * 1500;
sum += close3 * 300;
sum += open2 * 1500;
sum += half2 * 300;
sum += close2 * 50;
return sum;
}
int AlphaBetaPruning(int level, int alpha, int beta)
{
if (level == limit)
{
return evaluate(aiPlayer) - evaluate(userPlayer);
}
if (level % 2 == 0)
{
int max = -1000000;
int find = 0;
for (int i = 0; i < edgeCount; i++)
{
for (int j = 0; j < edgeCount; j++)
{
if (board[i, j] == 0)
{
board[i, j] = aiPlayer;
int e = AlphaBetaPruning(level + 1, alpha, beta);
board[i, j] = 0;
if (max < e)
{
max = e;
if (level == 0)
{
targetX = i;
targetY = j;
}
}
if (alpha < max)
{
alpha = max;
if (alpha >= beta) find = 1;
}
}
if (find == 1) break;
}
if (find == 1) break;
}
return max;
}
else
{
int min = 1000000;
int find = 0;
for (int i = 0; i < edgeCount; i++)
{
for (int j = 0; j < edgeCount; j++)
{
if (board[i, j] == 0)
{
board[i, j] = userPlayer;
int e = AlphaBetaPruning(level + 1, alpha, beta);
board[i, j] = 0;
if (min > e) min = e;
if (beta > min)
{
beta = min;
if (alpha >= beta) find = 1;
}
}
if (find == 1) break;
}
if (find == 1) break;
}
return min;
}
}
}
}